package com.sskj.login.ui.activity;


import android.annotation.SuppressLint;
import android.support.design.widget.BottomSheetDialog;
import android.text.TextUtils;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.LazyLoaders;
import com.github.yoojia.inputs.StaticScheme;
import com.github.yoojia.inputs.ValueScheme;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.TimeUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.AreaCodeEnum;
import com.sskj.lib.box.LiveDataBus;
import com.sskj.lib.box.verifier.ToastNextInputs;
import com.sskj.lib.util.BottomSheetUtil;
import com.sskj.lib.util.EditUtil;
import com.sskj.lib.util.SchemeUtil;
import com.sskj.lib.util.SendCodeUtil;
import com.sskj.login.R;
import com.sskj.login.R2;
import com.sskj.login.bean.rxbus.AccountBean;
import com.sskj.login.presenter.RegisterActivityPresenter;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.disposables.Disposable;

@Route(path = RConfig.LOGIN_REGISTER)//注册
public class RegisterActivity extends BaseActivity<RegisterActivityPresenter> {

    @BindView(R2.id.etAccount)
    EditText etAccount;
    @BindView(R2.id.etCode)
    EditText etCode;
    @BindView(R2.id.tvGetCheckCode)
    TextView tvGetCheckCode;
    @BindView(R2.id.etPwd)
    EditText etPwd;
    @BindView(R2.id.ivPwdToggle)
    ImageView ivPwdToggle;
    @BindView(R2.id.etPwdAgain)
    EditText etPwdAgain;
    @BindView(R2.id.ivPwdToggleAgain)
    ImageView ivPwdToggleAgain;
    @BindView(R2.id.etInviteCode)
    EditText etInviteCode;
    @BindView(R2.id.checkBox)
    CheckBox checkBox;
    @BindView(R2.id.tvAgreement)
    TextView tvAgreement;
    @BindView(R2.id.tvLogin)
    TextView tvLogin;
    @BindView(R2.id.btNext)
    TextView btNext;
    @BindView(R2.id.llCode)
    LinearLayout llcode;
    @BindView(R2.id.tvAreaCode)
    TextView tvAreaCode;
    @BindView(R2.id.tvCountry)
    TextView tvCountry;

    private Disposable timeDis;
    private AndroidNextInputs inputs;
    private AndroidNextInputs inputsCheckCode;
    private BottomSheetDialog priceModeSheet;
    private int type = 1;   //  1  手机号注册   2  邮箱注册

    @Override
    public boolean getImmersion() {
        return false;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.login_activity_register;
    }

    @Override
    public RegisterActivityPresenter getPresenter() {
        return new RegisterActivityPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    protected void initView() {


        inputs = new ToastNextInputs();
        inputs.clear();
        LazyLoaders lazyLoaders = new LazyLoaders(this);

        inputs
                .add(etAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.phone())
                .add(etPwd, SchemeUtil.notEmpty(etPwd), SchemeUtil.pwd())
                .add(etPwdAgain, SchemeUtil.notEmpty(etPwdAgain), SchemeUtil.equalsPwd(lazyLoaders.fromEditText(etPwd)))
                .add(etCode, SchemeUtil.notEmpty(etCode), SchemeUtil.smsCode());

        inputsCheckCode = new ToastNextInputs();
        inputsCheckCode.clear();
        inputsCheckCode
                .add(etAccount, SchemeUtil.notEmpty(etAccount), SchemeUtil.phone());

        ClickUtil.click(btNext, () -> {
            if (!inputs.test()) {
                return;
            }
            if (!checkBox.isChecked()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.login_registerActivity1));
                return;
            }
            mPresenter.register(
                    tvAreaCode.getText().toString(),
                    etAccount.getText().toString(),
                    etCode.getText().toString(),
                    etPwd.getText().toString(),
                    etPwdAgain.getText().toString(),
                    etInviteCode.getText().toString()
            );

        });

        ClickUtil.click(tvGetCheckCode, () -> {
            if (inputsCheckCode.test()) {
                tvGetCheckCode.setEnabled(false);
                mPresenter.sendCode(tvAreaCode.getText().toString(), etAccount.getText().toString());
            }
        });
        EditUtil.togglePs(etPwd, ivPwdToggle);
        EditUtil.togglePs(etPwdAgain, ivPwdToggleAgain);
        ClickUtil.click(tvAgreement, () -> {

            ARouter.getInstance().build(RConfig.APP_GUIDE_WEB).withBoolean(Constans.IS_REGISTER, true)
                    .navigation();
        });

        ClickUtil.click(llcode, () -> {
            if (priceModeSheet == null) {
                Flowable.fromIterable(AreaCodeEnum.list())
                        .map(areaCodeEnum -> areaCodeEnum.getDesc())
                        .toList()
                        .subscribe((strings, throwable) -> {

                            priceModeSheet = BottomSheetUtil.getBottomSheet(this, null, (recyclerView, position, v) -> {
                                priceModeSheet.dismiss();
                                tvAreaCode.setText(AreaCodeEnum.list().get(position).getCode());
                                tvCountry.setText(AreaCodeEnum.list().get(position).getDesc());
                            }, strings);
                        });
            }
            priceModeSheet.show();
        });
        ClickUtil.click(tvLogin, this::onBackPressed);

    }


    public void sendCheckCodeSuccess() {
        ToastUtil.showShort("发送验证码成功，请注意查收");
        DisposUtil.close(timeDis);
        timeDis = SendCodeUtil.timeCount(this, tvGetCheckCode);
    }

    @Override
    protected void onDestroy() {
        DisposUtil.close(timeDis);
        super.onDestroy();
    }

    public void sendCheckCodeFail() {
        tvGetCheckCode.setEnabled(true);
    }

    public void checkCodeSuccess() {
        ARouter.getInstance().build(RConfig.LOGIN_REGISTER)
                .withString(Constans.CODE, etCode.getText().toString())
                .withString(Constans.ACCOUNT, etAccount.getText().toString())
                .navigation();
        finish();
    }

    public void registerSuccess() {
        LiveDataBus.get().with(RxBusCode.REGISTER_ACCOUNT, AccountBean.class).postValue(new AccountBean(etAccount.getText().toString(), type));
        finish();
    }
}
